package com.kkb.controller;

import com.kkb.exception.PasswordErrorException;
import com.kkb.exception.UnameErrorException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("ex")
public class ExController {
    @RequestMapping("login")
    public ModelAndView login(){
        return new ModelAndView("login");
    }
    @RequestMapping("test01")
    public ModelAndView test01(String uname,String upassword) throws UnameErrorException, PasswordErrorException {
        if (!"liu".equals(uname)){
            throw new UnameErrorException("用户名输入错误");
        }
        if(!"123".equals(upassword)){
            throw new PasswordErrorException("密码输入错误");
        }
        int num = 1/0;
        return new ModelAndView("ok");
    }
//    @ExceptionHandler(value = {UnameErrorException.class,PasswordErrorException.class,Exception.class})
    public ModelAndView exhandler(Exception ex){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",ex.getMessage());
        if (ex instanceof UnameErrorException){
            modelAndView.setViewName("nameError");
        }else if(ex instanceof PasswordErrorException){
            modelAndView.setViewName("passwordError");
        }else{
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }
}
